module.exports = class extends think.Model {
  /**
   * 获取文章列表
   * @param where
   * @param page
   * @param size
   * @returns {Promise<any>}
   */
  async getPosts(where, page, size) {
    if (think.isEmpty(where)) {
      where = {type: 'post'};
    }
    for (const key in where) {
      if (think.isEmpty(where[key])) {
        delete where[key];
        continue;
      }
      if (key === 'author') {
        where.authorId = where[key];
        delete where[key];
      }

      if (key === 'keywords') {
        where['title|text'] = ['like', `%${where[key]}%`];
        delete where[key];
      }

      if (key === 'category') {
        // 查询出属于当前分类的 cid
        let cidList = await think.model('relationships').where({mid: where[key]}).getField('cid');
        if (think.isEmpty(cidList)) {
          cidList = [-1];
        }
        where['cid'] = ['in', cidList];
        delete where[key];
      }
    }

    const posts = await think.model('contents').where({type: 'post'}).where(where).page(page).limit(size).select();
    for (const post of posts) {
      post.created = think.datetime(post.created * 1000);
      post.categories = await think.model('metas').getPostCategoriesByCid(post.cid);
    }
    console.log(posts);
    return posts;
  }

  /**
   * 播入一篇内容
   * @param {*} content
   */
  async insertContent(content) {
    const nowTime = Number.parseInt(Date.now() / 1000);
    const insertStruct = {
      'title': think.isEmpty(content.title) ? '' : content.title,
      'created': think.isEmpty(content.created) ? nowTime : content.created,
      'modified': nowTime,
      'text': think.isEmpty(content.text) ? '' : content.text,
      'order': think.isEmpty(content.order) ? 0 : Number.parseInt(content.order),
      'authorId': content.authorId,
      'template': think.isEmpty(content.template) ? '' : content.template,
      'type': think.isEmpty(content.type) ? 'post' : content.type,
      'status': think.isEmpty(content.status) ? 'publish' : content.status,
      'password': think.isEmpty(content.password) ? '' : content.password,
      'commentsNum': think.isEmpty(content.commentsNum) ? 0 : content.commentsNum,
      'allowComment': !think.isEmpty(content.allowComment) && content.allowComment === 1 ? 1 : 0,
      'allowPing': !think.isEmpty(content.allowPing) && content.allowPing === 1 ? 1 : 0,
      'allowFeed': !think.isEmpty(content.allowFeed) && content.allowFeed === 1 ? 1 : 0,
      'parent': think.isEmpty(content.parent) ? 0 : Number.parseInt(content.parent)
    };

    /** 首先插入部分数据 */
    const insertId = await this.add(insertStruct);

    // TODO 更新缩略名

    return insertId;
  }

  /**
   * 获取已发布的文章数目
   */
  async getPublishedPostsNumber(uid) {
    return this.where({type: 'post', status: 'publish'}).count();
  }

  /**
   * 获取待审核的文章数目
   */
  async getWaitingPostsNumber(uid) {
    return this.where({type: ['IN', ['post', 'post_draft']]}).where({status: 'waiting'}).count();
  }

  /**
   * 获取草稿文章数目
   */
  async getDraftPostsNumber(uid) {
    return this.where({type: 'post_draft'}).count();
  }

  /**
   * 获取用户已发布的文章数目
   */
  async getPublishedPostsNumberByUid(uid) {
    return this.where({status: 'publish', authorId: uid, type: 'post'}).count();
  }

  /**
   * 获取用户待审核文章数目
   */
  async getWaitingPostsNumberByUid(uid) {
    return this.where({type: ['IN', ['post', 'post_draft']]}).where({status: 'waiting', authorId: uid}).count();
  }

  /**
   * 获取草稿页面数目
   */
  async getDraftPagesNumber() {
    return this.where({type: 'page_draft'}).count();
  }

  /**
   * 获取用户草稿文章数目
   */
  async getDraftPostsNumberByUid(uid) {
    return this.where({type: 'page_draft', authorId: uid}).count();
  }

  /**
   * 获取已发布页面数目
   */
  async getCategoriesNumber() {
    return this.where({type: 'page', status: 'publish'}).count();
  }
};
